<?php
class wapp_resource {
	public $app = '';
	public $src = '';
	public $type = '';	// js - loaded, css - loaded, html - loaded into body, img - preloaded
	public $group = '';
	public $state = 0; // -1 = error loading, 0 = not loaded, 1= queued, 2 = loading, 3 = loaded
//	public $size = 0; // needs to be read fro ma database not calcualted every time the up loads
//	public $md5 = ''; // needs to be read fro ma database not calcualted every time the up loads
}

class AppBase {
	public $resources = array();
	public $title = '';
	public $version = '';
	public $class = '';
	public $firstrun = false;
	public $firstrun_user = false;
	public $firstrun_instance = false;
	public $firstrun_session = false;
	public $been_polled = false; // polled this request
	public $last_polled = '';
	public $run_mode = 0; // 0 only when called, 1 session, 2 cross session

	// // add variable names to these array to allow them to be used directly
	// // Ex
	// $this->session_vars[] = 'pid';
	// // now $this->pid points directly to a session variable
	// // $this->xsession_vars is for cross session variables (not user specific)
	// // session variables take precedence if a variable name is found in both
	public $xsession_vars = array();
	public $session_vars = array();

	public function __construct(){
		global $wapi;
		$this->class = get_class($this);
		if (method_exists($this,'_construct'))
			$this->_construct();
	}
	public function load(){
		if (method_exists($this,'app_load'))
			return $this->app_load();
	}
    public function unload(){
		if (method_exists($this,'app_unload'))
			return $this->app_unload();
    }

	public function add_resource($type,$src,$group = ''){
		if ($src != '' && $type != '') {
			$res = new wapp_resource();
			$res->app = $this->class;
			$res->src = $src;
			$res->type = $type;
			$res->group = $group;
			$this->resources[] = $res;
		}
	}
	public function remove_resource($src){
		for ($i=0;$i<count($this->resources);$i++){
			if ($this->resources[i]->src == $src) {
				unset($this->resources[i]);
			}
		}
	}
	public function remove_resource_group($group){
		for ($i=0;$i<count($this->resources);$i++){
			if ($this->resources[i]->group == $group) {
				unset($this->resources[i]);
			}
		}
	}
	public function remove_resource_type($type){
		for ($i=0;$i<count($this->resources);$i++){
			if ($this->resources[i]->type == $type) {
				unset($this->resources[i]);
			}
		}
	}
	public function __destruct(){
		if (method_exists($this,'_destruct'))
			$this->_destruct();
	}
	public function user_logged_out(){
		if (method_exists($this,'logged_out'))
			$this->logged_out();
	}
	public function user_logged_in(){
		if (method_exists($this,'logged_in'))
			$this->logged_in();
	}
	function poll(){
		if ($this->been_polled)
			return false;
		$this->been_polled = true;
		if (method_exists($this,'app_poll')) {
			$hf = $this->has_focus;
			return $this->app_poll($this->has_focus);
		} else {
			return false;
		}
	}
    public function __get($name){
    	global $wapi;
        $fn_name = 'get_' . $name;
        if (method_exists($this, $fn_name)) {
            return $this->$fn_name();
        } else {
            //return $wapi->$name;
        	if (in_array($name,$this->session_vars)){
				$svar = $this->class . '_' . $name;
				if (isset($_SESSION[$svar])){
					return $_SESSION[$svar];
				}
			} elseif (in_array($name,$this->xsession_vars)){
				return $wapi->xget($name,$this->class);
			}
        }
        return null;
    }
    public function __set($name, $value){
		global $wapi;
        $fn_name = 'set_' . $name;
        if (method_exists($this, $fn_name)) {
            $this->$fn_name($value);
        } else {
        	if (in_array($name,$this->session_vars)){
				$svar = $this->class . '_' . $name;
				$_SESSION[$svar] = $value;
				return true;
			} elseif (in_array($name,$this->xsession_vars)){
				$wapi->xset($name,$value,$this->class);
				return true;
			}
        }
        return false;
    }
    private function get_has_focus(){
    	global $wapi;
    	if ($wapi->has_focus == $this->class){
    		return true;
    	}
    	return false;
    }
    private function get_path(){
    	global $wapi;
    	return $wapi->base_path . $wapi->apps_path . '/' . $this->class;
    }
    private function get_path_rel(){
    	global $wapi;
    	return $wapi->apps_path . '/' . $this->class;
    }
    private function get_file(){
    	global $wapi;
    	return $wapi->base_path . $wapi->apps_path . '/' . $this->class . '/app.php';
    }
    public function js($js){
    	global $wapi;
    	return $wapi->js($js);
    }
	public function wappLog($txt){
		global $wapi;
    	return $wapi->wappLog($txt);
	}
    private function get_db(){
    	global $wapi;
    	return $wapi->get_app_db($this->class,false);
    }
    private function get_db_user(){
    	global $wapi;
    	return $wapi->get_app_db($this->class,true);
    }
    // + cross session variable functions
    public function xset($var_name,$var_val) {
    	global $wapi;
    	return $wapi->xset($var_name,$var_val,$this->class);
    }
    public function xget($var_name) {
    	global $wapi;
    	return $wapi->xget($var_name,$this->class);
    }
    public function xunset($var_name) {
    	global $wapi;
    	return $wapi->xunset($var_name,$this->class);
    }
    public function user_xset($var_name,$var_val) {
    	global $wapi;
    	return $wapi->user_xset($var_name,$var_val,$this->class);
    }
    public function user_xget($var_name) {
    	global $wapi;
    	return $wapi->user_xget($var_name,$this->class);
    }
    public function user_xunset($var_name) {
    	global $wapi;
    	return $wapi->user_xunset($var_name,$this->class);
    }
    // - cross session variable functions
}
?>